/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import blueprint4j.utils.*;

import java.io.*;
import java.sql.*;
import java.util.*;

public class SettingsStoreDB extends SettingsStore {

	public static blueprint4j.utils.SettingsStore DBSET = null;

	static {
		try {
			DBSET = new SettingsStoreDB(DBTools.getDLC());
		} catch (Throwable th) {
			Log.critical.out(th);
		}
	}

	private DBConnection connection = null;

	public SettingsStoreDB(DBConnection connection) {
		this.connection = connection;
	}

	public boolean doesNameExist(String name) {
		try {
			return Setting.findSetting(DBTools.getLocalConnection(connection),name) != null;
		} catch (Exception exception) {
			exception.printStackTrace();
			Log.debug.out(exception);
		}
		return false;
	}

	public void set(String name, String value) {
		try {
			Setting setting = Setting.findSetting(DBTools.getDLC(),name);
			if (setting == null) {
				setting = new Setting(DBTools.getDLC());
			}
			setting.name.set(name);
			setting.value.set(value);
			setting.save();
		} catch (Exception exception) {
			Log.debug.out("Exception occurred while trying to set db setting",exception);
		}
	}

	public String get(String name) {
		try {
				Setting setting=Setting.findSetting(DBTools.getLocalConnection(connection),name);
				if (setting!=null){
					return setting.value.get();
				}
				return null;
		} catch (DataException de) {
			Log.debug.out("Exception occurred while trying to retrieve db setting",de);
		} catch (SQLException sqle) {
			Log.debug.out("Exception occurred while trying to retrieve db setting",sqle);
		}
		return null;
	}

	public void save() {}

	public Enumeration getSettingNames() {
		try {
			Properties properties = new Properties();
			for (Setting setting = Setting.getAllSettings(DBTools.getDLC());setting != null;setting = (Setting) setting.getNextBindable()) {
				properties.setProperty(setting.name.get(),setting.value.get());
			}
			return properties.propertyNames();
		} catch (Exception exception) {
			Log.debug.out("Exception occurred while trying to retrieve db setting",exception);
			return null;
		}
	}
}

